package com.inossem.warehousestorage_inossem.activity.allocation.out_storage;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.allocation.out_storage.extra.AllocationOutStorageExtra;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.bean.allocation.AllocationOutStorageBaseInfoBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.allocation.AllocationOutStorageBaseInfoRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.BaseInteface;

import java.io.Serializable;
import java.util.List;

/**
 * 创建调拨
 *
 * @author 詹建宇
 * @time on 2018/3/28 10:19
 * @email jianyu.zhan@inossem.com
 */
public class AllocationOutCreatDocumentActivity extends BaseActivity implements BaseInteface, View.OnClickListener {

    private TextView title, chooseOutPlaceTv, chooseInPlaceTv, chooseDocumentTv, nextStep;
    private List<AllocationOutStorageBaseInfoBean.BaseInfoBodyBean.FtyListBean.LocationAryBean> outFtys, inFtys;//库存地点
    private List<AllocationOutStorageBaseInfoBean.BaseInfoBodyBean.InputOrderListBean> inputOrderList;//生产入库单列表
    private static final int REQUESTCODE = 100;
    public static final int RESULTCODE = 101;
    private String fty_id;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        super.onCreateImpl(savedInstanceState);
        setContentView(R.layout.cy_activity_out_storage_allocating_creatdocument);

        initView(this);
        initData(this);
    }

    @Override
    public void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        chooseOutPlaceTv = (TextView) activity.findViewById(R.id.chooseOutPlaceTv);
        chooseInPlaceTv = (TextView) activity.findViewById(R.id.chooseInPlaceTv);
        chooseDocumentTv = (TextView) activity.findViewById(R.id.chooseDocumentTv);
        nextStep = (TextView) activity.findViewById(R.id.nextStep);

        chooseOutPlaceTv.setOnClickListener(this);
        chooseInPlaceTv.setOnClickListener(this);
        chooseDocumentTv.setOnClickListener(this);
        nextStep.setOnClickListener(this);
    }

    @Override
    public void initData(Activity activity) {
        title.setText(activity.getResources().getString(R.string.cy_activity_out_storage_allocating_creat_allocating));
        ActivityUtils.initBackAndFunction(activity, R.string.activity_receive_result_back, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });
        chooseDocumentTv.setTag("");
        getData(activity);
    }

    private void getData(Activity activity) {
        AllocationOutStorageBaseInfoRequest.request(activity, new GsonBeanListener<AllocationOutStorageBaseInfoBean>() {
            @Override
            public void onSuccess(AllocationOutStorageBaseInfoBean bean) {
                outFtys = bean.getBody().getFty_output_list().get(0).getLocation_ary();
                inFtys = bean.getBody().getFty_input_list().get(0).getLocation_ary();
                inputOrderList = bean.getBody().getInput_order_list();
                fty_id = bean.getBody().getFty_output_list().get(0).getFty_id();
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.chooseOutPlaceTv://发出库存地点
                DialogUtils.getAllocationCreatDocumentFtyDialog(this, outFtys, new DialogUtils.AllocationCreatDocumentFtyListener() {
                    @Override
                    public void onSelect(Dialog dialog, String id, String code, String name) {
                        chooseOutPlaceTv.setText(name);
                        chooseOutPlaceTv.setTag(id);
                    }
                }).show();
                break;
            case R.id.chooseInPlaceTv://接收库存地点
                DialogUtils.getAllocationCreatDocumentFtyDialog(this, inFtys, new DialogUtils.AllocationCreatDocumentFtyListener() {
                    @Override
                    public void onSelect(Dialog dialog, String id, String code, String name) {
                        chooseInPlaceTv.setText(name);
                        chooseInPlaceTv.setTag(id);
                    }
                }).show();
                break;
            case R.id.chooseDocumentTv://生产入库单
                startActivityForResult(new Intent(this, AllocationOutChooseInOrderActivity.class).putExtra(AllocationOutStorageExtra.ALLOCATION_INORDER_LIST, (Serializable) inputOrderList), REQUESTCODE);
                break;
            case R.id.nextStep://下一步
                String message = check(chooseOutPlaceTv.getText().toString(), chooseInPlaceTv.getText().toString());
                if (!TextUtils.isEmpty(message)) {
                    Utils.middleToast(this, message);
                    return;
                }
                if (TextUtils.isEmpty((String) chooseDocumentTv.getTag())) {
                    //没有生产入库单
                    Intent intent = new Intent(this, AllocationOutMaterialsHaveSearchActivity.class);
                    intent.putExtra(AllocationOutStorageExtra.ALLOCATION_LOCATION_ID_OUTPUT, (String) chooseOutPlaceTv.getTag());
                    intent.putExtra(AllocationOutStorageExtra.ALLOCATION_LOCATION_ID_INPUT, (String) chooseInPlaceTv.getTag());
                    intent.putExtra(AllocationOutStorageExtra.ALLOCATION_LOCATION_NAME_INPUT, chooseInPlaceTv.getText().toString().trim());
                    intent.putExtra(AllocationOutStorageExtra.FTY_ID, String.valueOf(fty_id));
                    startActivity(intent);
                } else {
                    //有生产入库单
                    Intent intent = new Intent(this, AllocationOutMaterialsNoSearchActivity.class);
                    intent.putExtra(AllocationOutStorageExtra.ID, (String) chooseDocumentTv.getTag());
                    intent.putExtra(AllocationOutStorageExtra.ALLOCATION_TYPE, "1");
                    startActivity(intent);
                }
                break;
        }
    }

    @Override
    protected void onActivityResultImpl(int requestCode, int resultCode, Intent data) {
        super.onActivityResultImpl(requestCode, resultCode, data);
        if (data == null) return;
        if (requestCode == REQUESTCODE & resultCode == RESULTCODE) {
            AllocationOutStorageBaseInfoBean.BaseInfoBodyBean.InputOrderListBean bean = (AllocationOutStorageBaseInfoBean.BaseInfoBodyBean.InputOrderListBean) data.getSerializableExtra(AllocationOutStorageExtra.ALLOCATION_INORDER_LIST_BEAN);
            chooseDocumentTv.setText(bean.getStock_input_code());
            chooseDocumentTv.setTag(bean.getStock_input_id());

        }
    }

    private String check(String outFty, String inFty) {
        String message = "";
        if (TextUtils.isEmpty(outFty) || outFty.endsWith(getResources().getString(R.string.activity_inventory_please_choose))) {
            message = "请选择发出库存地点";
            return message;
        }

        if (TextUtils.isEmpty(inFty) || outFty.endsWith(getResources().getString(R.string.activity_inventory_please_choose))) {
            message = "请选择接收库存地点";
            return message;
        }

        return message;
    }

}
